<?php
/**
 * 后台品牌管理
 */
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class brand extends admin {
	private $db;
	public $siteid;
	function __construct() {
		parent::__construct();
		$this->db = pc_base::load_model('brand_model');
		$this->siteid = $this->get_siteid();
	}
	
	/**
	 * 品牌管理列表
	 */
	public function init()
	{
		$catModel = pc_base::load_model('category_model');
		
		$where = '1';
		if (isset($_GET['info']['catid_1']) && $_GET['info']['catid_1']>0) {
			if (isset($_GET['info']['catid_2']) && $_GET['info']['catid_2']>0) {
				$where .= ' AND `catid`='.$_GET['info']['catid_2'];
			} else {
				$cat_1 = $catModel->get_one("`catid`=".$_GET['info']['catid_1'], 'arrchildid');
				$where .= ' AND `catid` IN('.$cat_1['arrchildid'].')';
			}
		}
		
		if (isset($_GET['info']['userid']) && $_GET['info']['userid']>0) {
			$where .= ' AND `userid`='.$_GET['info']['userid'];
		}
		
		$order = 'sort DESC, parentid ASC';
		$page = (isset($_GET['page']) && $_GET['page']>0) ? $_GET['page'] : 1;
		$pagesize = 20;
		$infos = $this->db->listinfo($where, $order, $page, $pagesize);
		$pages = $this->db->pages;
		
		$memberModel = pc_base::load_model('member_model');
		//栏目
		$cats = array();
		$first_cats = $catModel->select(array('parentid' => TOP_PRODUCT_CATID), 'catid, catname');
		foreach ($first_cats AS $v) {
			$v['child'] = $catModel->select(array('parentid' => $v['catid']), 'catid, catname');
			if ($v['child']) {
				$cats[] = $v;
			}
		}
		if (!$cats) {
			showmessage('请先添加栏目', WEB_PATH.'/index.php?m=admin&c=category', '', 'add');
		}
		$cats_json = json_encode($cats);
		
		
		//会员
		$where = '`groupid`=6 AND `vip`=1 AND `overduedate`>'.SYS_TIME;
		$members = $memberModel->select($where, 'userid, username', '', 'username ASC');
		
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=brand&c=brand&a=add\', title:\'添加品牌\', width:\'560\', height:\'400\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', '添加品牌');
		include $this->admin_tpl('brand_manage');
	}
	
	/**
	 * 添加
	 */
	public function add()
	{
		$catModel = pc_base::load_model('category_model');
		$memberModel = pc_base::load_model('member_model');
		if (isset($_POST['info']['name'])) {
			$catid_1 = intval($_POST['info']['catid_1']);
			$catid_2 = intval($_POST['info']['catid_2']);
			$cat_1 = $catModel->get_one(array('catid'=>$catid_1), 'catid,catname');
			$cat_2 = $catModel->get_one(array('catid'=>$catid_2), 'catid,catname');
			$user = $memberModel->get_one(array('userid'=>$_POST['info']['userid']), 'userid,username,modelid');
			$memberModel->set_model($user['modelid']);
			$user = $memberModel->get_one(array('userid'=>$_POST['info']['userid']));
			$data = array(
				'userid' => $user['userid'],
				'username'	=> $user['username'],
				'catid'		=> $catid_2,
				'top_catname'	=> $cat_1['catname'].'-'.$cat_2['catname'],
				'name'			=> $_POST['info']['name'],
				'parentid'		=> 0,
				'create_time'	=> SYS_TIME,
				'pcompanyname'	=> $_POST['info']['pcompanyname'] ? $_POST['info']['pcompanyname']:$user['companyname'],
				'prealname'	=> $_POST['info']['prealname'] ? $_POST['info']['prealname']:$user['realname'],
				'pqq'	=> $_POST['info']['pqq'] ? $_POST['info']['pqq']:$user['qq'],
				'ptel'	=> $_POST['info']['ptel'] ? $_POST['info']['ptel']:$user['tel'],
				'paddress'	=> $_POST['info']['paddress'] ? $_POST['info']['paddress']:$user['address'],
			);
			
			if ($bid = $this->db->insert($data, true)) {
				$this->db->update(array('sort'=>$bid), 'id='.$bid);
				//添加子类
				if (!empty($_POST['info']['child'])) {
					$_POST['info']['child'] = str_replace('，', ',', $_POST['info']['child']);
					$childs = explode(',', $_POST['info']['child']);
					foreach ($childs as $v) {
						$data['parentid'] = $bid;
						$data['name'] = trim($v);
						$data['sort'] = $bid;
						$this->db->insert($data);
					}
				}
				showmessage(L('operation_success'), '', '', 'add');
			}
		}
		
		//栏目
		$cats = array();
		$first_cats = $catModel->select(array('parentid' => TOP_PRODUCT_CATID), 'catid, catname');
		foreach ($first_cats AS $v) {
			$v['child'] = $catModel->select(array('parentid' => $v['catid']), 'catid, catname');
			if ($v['child']) {
				$cats[] = $v;
			}
		}
		if (!$cats) {
			showmessage('请先添加栏目', '/index.php?m=admin&c=category', '', 'add');
		}
		$cats_json = json_encode($cats);
		
		//会员
		$where = '`groupid`=6 AND `vip`=1 AND `overduedate`>'.SYS_TIME;
		$members = $memberModel->select($where, 'userid, username', '', 'username ASC');
		
		$show_header = true;
		$show_validator = true;
		include $this->admin_tpl('brand_add');
	}
	
	/**
	 * 添加分类
	 */
	public function addClass()
	{
		if (!intval($_GET['bid'])) {
			showmessage('品牌不存在', '', '', 'add');
		}
		
		$brand = $this->db->get_one(array('id'=>intval($_GET['bid'])));
		if (isset($_POST['info']['name'])) {
			$catModel = pc_base::load_model('category_model');
			$data = array(
				'userid' => $brand['userid'],
				'username'	=> $brand['username'],
				'catid'		=> $brand['catid'],
				'top_catname'	=> $brand['top_catname'],
				'name'			=> $_POST['info']['name'],
				'parentid'		=> $brand['id'],
				'sort'			=> $brand['id'],
				'create_time'	=> SYS_TIME
			);
			
			if ($this->db->insert($data)) {
				showmessage(L('operation_success'), '', '', 'addClass');
			}
		}
		
		
		$show_header = true;
		$show_validator = true;
		include $this->admin_tpl('brand_addClass');
	}
	
	/**
	 * 编辑品牌
	 * 
	 */
	public function edit()
	{
		$bid = intval($_GET['bid']);
		$brand = $this->db->get_one('`id`='.$bid);
		$catModel = pc_base::load_model('category_model');
		$memberModel = pc_base::load_model('member_model');
		
		if ($_GET['dosubmit'] && $_POST['info']['name']) {
			$update_data = array(
				'name'		=>	$_POST['info']['name'],
			);
			//栏目
			if ($_POST['info']['catid_2'] != $brand['catid']) {
				$cat_2 = $catModel->get_one('catid='.$_POST['info']['catid_2'], 'catid,catname,parentid');
				$cat_1 = $catModel->get_one('catid='.$cat_2['parentid'], 'catid,catname,parentid');
				$update_data['catid'] = $cat_2['catid'];
				$update_data['top_catname'] = $cat_1['catname'].'-'.$cat_2['catname'];
			}
			//会员
			$user = $memberModel->get_one(array('userid'=>$_POST['info']['userid']), 'userid,username,modelid');
			$memberModel->set_model($user['modelid']);
			$user = array_merge($user,$memberModel->get_one(array('userid'=>$_POST['info']['userid'])));
			$update_data['userid'] = $user['userid'];
			$update_data['username'] = $user['username'];
			$update_data['pcompanyname'] = ($_POST['info']['pcompanyname'] ? $_POST['info']['pcompanyname']:$user['companyname']);
			$update_data['prealname']	= ($_POST['info']['prealname'] ? $_POST['info']['prealname']:$user['realname']);
			$update_data['pqq']	= ($_POST['info']['pqq'] ? $_POST['info']['pqq']:$user['qq']);
			$update_data['ptel'] = ($_POST['info']['ptel'] ? $_POST['info']['ptel']:$user['tel']);
			$update_data['paddress'] = ($_POST['info']['paddress'] ? $_POST['info']['paddress']:$user['address']);
			
			$this->db->update($update_data, array('id'=>$bid));
			//更新子分类
			unset($update_data['name']);
			$this->db->update($update_data, array('parentid'=>$bid));
			
			//更新关联产品信息 
			if (isset($cat_2['catid']) && $cat_2['catid']>0) {
				$this->db->updateRelativeProduct($brand['id'], $cat_2['catid']);
			}

			showmessage('操作成功！', '', '', 'edit');
		}
		
		//栏目
		$cats = array();
		$first_cats = $catModel->select(array('parentid' => TOP_PRODUCT_CATID), 'catid, catname');
		foreach ($first_cats AS $v) {
			$v['child'] = $catModel->select(array('parentid' => $v['catid']), 'catid, catname');
			if ($v['child']) {
				$cats[] = $v;
			}
		}
		if (!$cats) {
			showmessage('请先添加栏目', '/index.php?m=admin&c=category', '', 'add');
		}
		$cats_json = json_encode($cats);
		
		//会员
		$where = '`groupid`=6 AND `vip`=1 AND `overduedate`>'.SYS_TIME;
		$members = $memberModel->select($where, 'userid, username', '', 'username ASC');
		
		$cat_2 = $catModel->get_one('catid='.$brand['catid'], 'catid,catname,parentid');
		$cat_1 = $catModel->get_one('catid='.$cat_2['parentid'], 'catid,catname,parentid');
		$show_header = true;
		$show_validator = true;
		include $this->admin_tpl('brand_edit');
	}
	
	/**
	 * 更新分类
	 */
	public function editClass()
	{
		$bid = intval($_GET['bid']);
		$brand = $this->db->get_one('`id`='.$bid);

		if (isset($_POST['info']['name']) && !empty($_POST['info']['name'])) {
			$this->db->update(array('name'=>$_POST['info']['name']), '`id`='.$bid);
			showmessage('操作成功 ！', '', '', 'editClass');
		}
		$show_header = true;
		$show_validator = true;
		include $this->admin_tpl('brand_editClass');
	}
	
	/**
	 * 删除品牌
	 */
	public function delete()
	{
		$bid = intval($_GET['bid']);
		if ($bid > 0) {
			$brands = $this->db->select('`id`='.$bid.' OR `parentid`='.$bid, 'id');
			$ids = $bid;
			foreach ($brands AS $v) {
				$ids .= ','.$v['id'];
			}
			if (!empty($ids)) {
				$content_model = pc_base::load_model('content_model');
				$content_model->set_model(13);  //只针对产品模型
				$content_model->update(array('bids'=>""), '`bids` IN('.$ids.')');
			}
			
			$this->db->delete("`id`=".$bid." or `parentid`=".$bid);
			showmessage('操作成功！', APP_PATH.'index.php?m=brand&c=brand');
		} else {
			showmessage('操作失败！', APP_PATH.'index.php?m=brand&c=brand');
		}
	}
	
	/**
	 * 更新品牌缓存
	 */
	public function cache()
	{
		set_time_limit(0);
		$user_db = pc_base::load_model('member_model');
		$cat_db = pc_base::load_model('category_model');
		$brands = $this->db->select();
		foreach ($brands AS $v) {
			if ($v['userid']=="") {
				$this->db->delete('`id`='.$v['id'].' OR `parentid`='.$v['id']);
				continue;
			}
			
			$update_data = array();
			if ($v['userid'] && $v['pcompanyname']=='' && $v['prealname']=='' && $v['ptel']=='' && $v['paddress']=='') {
				$user = $user_db->get_one(array('userid'=>$v['userid']), 'userid,username,modelid');
				$user_db->set_model($user['modelid']);
				$user = array_merge($user, $user_db->get_one(array('userid'=>$v['userid'])));
				$user_db->set_model("");
				
				$update_data = array(
					'pcompanyname'	=>	$user['companyname'],
					'prealname'		=>	$user['realname'],
					'pqq'			=>	$user['qq'],
					'paddress'		=>	$user['address'],
					'ptel'			=>	$user['tel'],
				);
			}
			
			if ($v['catid']) {
				$cat_2 = $cat_db->get_one(array('catid'=>$v['catid']), 'catid,catname,parentid,arrparentid');
				$update_data['top_catname'] = $cat_2['catname'];
				if (count(explode(',', $cat_2['arrparentid'])) == 3) {
					$cat_1 = $cat_db->get_one(array('catid'=>$cat_2['parentid']), 'catid,catname,arrparentid');
					$update_data['top_catname'] = $cat_1['catname'] . ' > ' . $cat_2['catname'];
				}
			}
			
			if ($update_data) {
				$this->db->update($update_data, '`id`='.$v['id']);
			}
		}
		
		showmessage('更新成功！', APP_PATH.'index.php?m=brand&c=brand&a=init');
	}
}